<?php
  include_once('./queries/utils.inc.php');

  function job_Queue_Global_Summary($connection) {
    $sql = "SELECT l.description as Status, count(*) " .
           "FROM clusterads_horizontal c, procads_horizontal p, l_jobstatus l " .
           "WHERE c.cluster_id=p.cluster_id " .
             "AND c.scheddname=p.scheddname " .
             "AND COALESCE(p.jobstatus, 1)=l.jobstatus " .
           "GROUP BY l.jobstatus, l.description " .
		"ORDER BY l.jobstatus ";
	$rcount = get_recordcount($connection, $sql);
    
    $fields = array();
    $fields[0] = "Status";
    $fields[1] = "Job Count";
    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));

	return $rs;

  }

  function job_Queue_Global_User_Count($connection) {
    $sql = "SELECT count(distinct(c.owner)) " .
           "FROM clusterads_horizontal c";
    
    return get_scalar(get_recordset($connection, $sql));

  }  

  function job_Queue_By_User($connection, $index, $pn) { 
    $sql = "SELECT c.owner, l.description as STATUS, count(*) " .
           "FROM clusterads_horizontal c, procads_horizontal p, l_jobstatus l " .
           "WHERE c.scheddname=p.scheddname " .
             "AND c.cluster_id=p.cluster_id " . 
             "AND COALESCE(p.jobstatus, 1)=l.jobstatus " .
           "GROUP BY c.owner, l.jobstatus, l.description " .
		"ORDER BY l.jobstatus ";
    
		//$rcount = get_recordcount($connection, $sql);
	//$sql = $sql . get_limitclause($pn, $rcount) . ";";

    //echo $sql;
    $raw_data = get_recordset($connection, $sql);
    //var_dump($raw_data);
    $users = array(); $userCounter = 0;
    $statuses = array(); $statusCounter = 1;
    foreach ($raw_data as $record) {
      //var_dump($record);
      $curUser = $record[0];
      $curStatus = $record[1];
      $curCount = $record[2];

      if (!isset($users["{$curUser}"])) {
        //this is the first time we've seen this user
        $users["{$curUser}"] = $userCounter;
        $userCounter++;
      }

      if (!isset($statuses["{$curStatus}"])) {
        //this is the first time we've seen this status
        $statuses["{$curStatus}"] = $statusCounter;
        $statusCounter++;
      }
    }
    //var_dump($users);
    //var_dump($statuses);
    //could do it in one pass, but would rather make it easier to
    //implement sorting on users and statuses later...
    ksort($users);
    $newIndexCounter = 0;
    foreach($users as $user => $userIndex) {
      //echo "user: " . $user . ", userIndex: " . $userIndex . "\n";
      $users["{$user}"] = $newIndexCounter;
      $newIndexCounter++;
      
    }
    //var_dump($users);
    $new_data = array();
    //echo "This is first:";
    //var_dump($new_data);
    foreach ($raw_data as $record) {
      $curUser = $record[0];
      $curStatus = $record[1];
      $curCount = $record[2];
      $uIndex = $users["{$curUser}"];
      $sIndex = $statuses["{$curStatus}"];
      //make sure that user is initialized
      if (!isset($new_data[$uIndex])) { 
        $new_data[$uIndex] = array();
        $new_data[$uIndex][0] = $curUser;
      }
      //echo "uIndex: {$uIndex},sIndex: {$sIndex}\n"; 
      $new_data[$uIndex][$sIndex] = $curCount;
    }

/*
    $statusTotals = array();
    for ($j=1; $j<$statusCounter;$j++) {
      $statusTotals[$j] = 0;
    }
*/
    for ($i = 0; $i < count($new_data); $i++) {
      //this is each user
      $userTotal = 0;
      for ($j = 1; $j < $statusCounter; $j++) {
        //this is each status
        if (!isset($new_data[$i][$j])) $new_data[$i][$j] = 0;
        $userTotal += $new_data[$i][$j];
			//$statusTotals[$j] += $new_data[$i][$j];
      }
      $new_data[$i][$statusCounter]="<b>" . $userTotal . "</b>"; 
    }
		//$new_data[$userCounter] = array();
		//$new_data[$userCounter][0] = "<b>Total</b>";
/*
    $grandTotal = 0;
    for ($j=1;$j<$statusCounter;$j++) {
      $grandTotal += $statusTotals[$j];
      $new_data[$userCounter][$j] = "<b>" . $statusTotals[$j] . "</b>";      
    }
    $new_data[$userCounter][$statusCounter]="<b><i>" . $grandTotal . "</i></b>";
*/
    //var_dump($new_data);

    //echo $index;
    unset($_SESSION["{$index}"]);
    $_SESSION["{$index}"] = array();
    $_SESSION["{$index}"]["title"] = "Job Queue by owner";
    $_SESSION["{$index}"]["message"] = "Job Queue by owner";

    $fields = array();
    $fields[0] = "Owner";
    foreach ($statuses as $status => $statusIndex) {
      $fields[$statusIndex] = $status;
    }
    $fields[$statusCounter] = "Total";

    $rs = new recordset($userCounter, $fields, $new_data);
    return $rs;
  }
?>
